import { useMemo } from 'react'
import useChartDataInfo from '../useChartDataInfo'

function useCategoryAxis(widgetId, axis) {
  const { data = [], indexToFieldArray = [], categoryKey } = useChartDataInfo(widgetId)

  const categoryAxis = useMemo(() => {
    const getIndex = (id) => indexToFieldArray.findIndex((v) => id === v)
    const getCategory = (index) => ({
      type: 'category',
      data: data.map((v) => v[index])
    })

    const categoryIndex = getIndex(categoryKey)
    const categoriesAxis = [getCategory(categoryIndex)]

    return categoriesAxis
  }, [data, indexToFieldArray, categoryKey])

  return categoryAxis
}

export default useCategoryAxis
